package gwtool.container;

import java.util.ArrayList;
import java.util.List;

public class DataSet implements Comparable<DataSet> {

	private int id = 0;

	private int gruppe = 0;

	private int version = 0;

	private int onlineVersion = 0;

	private List<DataSetItem> itemList = new ArrayList<>();

	private boolean changed = false;

	private boolean deleted = false;

	private static String[] moeglicheTypen = { "Gerät", "Prüfung", "Person", "Lehrgang" };

	public DataSet(int id, int onlineVersion, int gruppe) {

		this.id = id;
		this.onlineVersion = onlineVersion;
		this.gruppe = gruppe;
		this.version = onlineVersion;
	}

	public DataSet(int id, int onlineVersion, int gruppe, List<DataSetItem> list) {

		this.id = id;
		this.onlineVersion = onlineVersion;
		this.itemList = list;
		this.gruppe = gruppe;
		this.version = onlineVersion;
	}

	public DataSet() {

	}

	public int getId() {

		return id;
	}

	public void setId(int id) {

		this.id = id;
	}

	public int getGruppe() {

		return gruppe;
	}

	public void setGruppe(int gruppe) {

		this.gruppe = gruppe;
	}

	public int getVersion() {

		return version;
	}

	public void setVersion(int version) {

		this.version = version;
	}

	public int getOnlineVersion() {

		return onlineVersion;
	}

	public void setOnlineVersion(int onlineVersion) {

		this.onlineVersion = onlineVersion;
	}

	public List<DataSetItem> getItemList() {

		return itemList;
	}

	public void addNewDataSetItem(DataSetItem item) {

		int newID = getMaxId() + 1;
		item.setId(newID);
		item.setLocalErstellt(true);
		getItemList().add(item);
		version = newID;
		setChanged(true);
	}

	public void setItemList(List<DataSetItem> itemList) {

		this.itemList = itemList;
	}

	public boolean isChanged() {

		return changed;
	}

	public void setChanged(boolean changed) {

		this.changed = changed;
	}

	public boolean isDeleted() {

		return deleted;
	}

	public int isDeletedInt() {

		if (deleted) {
			return 1;
		}
		return 0;
	}

	public void setDeleted(boolean deleted) {

		this.deleted = deleted;
	}

	public String getType() {

		if (itemList.get(0) != null) {
			return itemList.get(0).getType();
		}
		return null;
	}

	public DataSetItem getDataItemByID(int id) {

		for (DataSetItem i : getItemList()) {
			if (i.getId() == id) {
				return i;
			}
		}
		return null;
	}

	public DataSetItem getAktuellesDataItem() {

		for (DataSetItem i : getItemList()) {
			if (i.getId() == version) {
				return i;
			}
		}
		return null;
	}

	private int getMaxId() {

		int maxId = -1;
		for (DataSetItem i : getItemList()) {
			if (i.getId() > maxId) {
				maxId = i.getId();
			}
		}
		return maxId;
	}

	@Override
	public String toString() {

		return getAktuellesDataItem().toString();
	}

	public static Object[] getMoeglicheTypen() {

		return moeglicheTypen;
	}

	public static DataSetItem createNewItemByType(String s) {

		if (s.equals("Gerät")) {
			return new GeraetItem();
		} else if (s.equals("Prüfung")) {
			return new OnlinePruefungItem();
		} else if (s.equals("Person")) {
			return new PersonItem();
		} else if (s.equals("Lehrgang")) {
			return new OnlineLehrgangItem();
		} else {
			throw new RuntimeException("type" + s + " nicht bekannt");
		}
	}

	@Override
	public int compareTo(DataSet set) {

		return getAktuellesDataItem().getBezeichner().compareTo(set.getAktuellesDataItem().getBezeichner());
	}
}
